<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>客户管理</el-breadcrumb-item>
      <el-breadcrumb-item>优惠券记录</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7" style="width: 500px">
          <el-input placeholder="请输入优惠券名称或客户名字" v-model="query" style="width: 200px"/>
          <el-button type="primary" @click="search"> <i class="el-icon-search"></i>搜索</el-button>
          <el-button type="info" @click="refresh">重置</el-button>
        </el-col>
      </el-row>
      <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%">
        <el-table-column
            fixed
            prop="id"
            label="id"
            align="center"
            width="50"
        >
        </el-table-column>
        <el-table-column
            prop="couponName"
            label="优惠券名称"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            prop="amount"
            label="优惠券金额"
            align="center"
            width="100"
        >
        </el-table-column>
        <el-table-column
            prop="clientName"
            label="客户名称"
            align="center"
        >
        </el-table-column>
        <el-table-column
            prop="code"
            label="优惠券码"
            align="center"
        >
        </el-table-column>
        <el-table-column
            prop="startTime"
            label="开始时间"
            align="center"
            width="200"
        >
        </el-table-column>
        <el-table-column
            prop="endTime"
            label="结束时间"
            align="center"
            width="200">
        </el-table-column>
        <el-table-column
            prop="useTime"
            label="使用时间"
            align="center"
            width="120">
        </el-table-column>
        <el-table-column
            prop="orderSn"
            label="订单编号"
            align="center"
        >
        </el-table-column>
        <el-table-column
            prop="createTime"
            label="记录时间"
            align="center"
            width="80">
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            align="center"
        >
          <template slot-scope="scope">
            <el-button type="danger" icon="el-icon-delete"  size="medium" circle @click="delCouponRecord(scope.row.id)"></el-button>
          </template>
        </el-table-column>

      </el-table>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[1, 3, 5, 7]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "couponRecord",
  data(){
    return{
      //查询条件
      query:"",
      //当前显示的页码
      currentPage: 1,
      //总条数
      total: 0,
      //每页显示的记录
      pageSize: 5,
      //当前的记录数据
      tableData: [],
    }
  },
  created() {
    this.initCouponRecordTable(this.currentPage,this.pageSize);
  },
  methods:{
    initCouponRecordTable(currentPage,pageSize){
      var that=this;
      this.$http.post(`/customer/couponHistory/findCouponHistoryByPage/${currentPage}/${pageSize}?query=${this.query}`).then(function (resp){
        that.tableData=resp.data.data.records;
        that.total=resp.data.data.total;
      })
    },
    //切换每页显示的条数
    handleSizeChange(val) {
      this.pageSize=val;
      this.initCouponRecordTable(this.currentPage,this.pageSize);
    },
    //切换当前页码
    handleCurrentChange(val) {
      this.currentPage=val;
      this.initCouponRecordTable(this.currentPage,this.pageSize);
    },
    //搜索
    search(){
      this.currentPage=1,
      this.pageSize=5,
      this.initCouponRecordTable(this.currentPage,this.pageSize)
    },
    //刷新局部页面
    refresh (){
      this.query='';
      this.initCouponRecordTable(1,this.pageSize);
    },
    delCouponRecord(id){
      var that=this;
      this.$confirm('确定删除吗？, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete(`/customer/couponHistory/delcouponRecord/${id}`).then(function (resp) {
          if (resp.data.code!=200){
            return that.$message.error("删除失败");
          }else {
            if (that.total%that.pageSize==1){
              that.currentPage--;
            }else {
              that.currentPage = that.currentPage;
            }
            that.$message.success("删除成功");
            that.initCouponRecordTable(that.currentPage,that.pageSize);
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  }

}
</script>

<style scoped>

</style>